import { useCallback } from "react";
import { useSelector, useDispatch } from "react-redux"
import Creators from "../store/actions"
import CommodityApi from "../api/commodity"

// 拿到抢购的api
// import seckillApi from "../api/seckill"

export const useAdd_info = () => {
    const commodityImage = useSelector(state => state.commodityImage)

    const { _id, name, desc, guarantee, aftersale, storeCondition, place, specs,
        pack, sales, image, detailsImage
    } = commodityImage

    const dispatch = useDispatch()

    // 将要新增的数据放入状态机
    const addCommodityImage = useCallback((data) => {
        // 将拿到的对象传入状态机
        dispatch(Creators.commodityAddResinfo({ data }))
    }, [dispatch]);

    const delCommodityImage = useCallback(async (_id, url) => {
        const data = await CommodityApi.delImgs(_id, url);
        dispatch(Creators.commodityDelResinfo({ data }))
    }, [dispatch]);

    // 删除商品详情图片
    const delCommDetailsImage = useCallback(async (_id, url) => {
        const data = await CommodityApi.delDetailsImgs(_id, url);
        console.log(data);
        dispatch(Creators.commodityDelResinfo({ data }))
    }, [dispatch]);




    return [{
        _id,
        name, desc, guarantee, aftersale, storeCondition, place, specs,
        pack, sales, image, detailsImage
    }, { addCommodityImage, delCommodityImage, delCommDetailsImage }]
}